package com.rc.saas.tenant.controller.hlht;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.elec.ElecGun;
import com.rc.saas.tenant.model.elec.ElecPile;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpen;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.hlht.HlhtTenantOpenPileSearch;
import com.rc.saas.tenant.service.elec.ElecGunService;
import com.rc.saas.tenant.service.elec.ElecPileService;
import com.rc.saas.tenant.service.hlht.HlhtTenantOpenGunService;
import com.rc.saas.tenant.service.hlht.HlhtTenantOpenPileService;
import com.rc.saas.tenant.service.hlht.HlhtTenantOpenService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.vo.hlht.HlhtTenantOpenPileVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/8/14
 */
@Controller
@RequestMapping("/hlhtTenantOpenPile")
public class HlhtTenantOpenPileController {
    private static Logger logger = LoggerFactory.getLogger(HlhtTenantOpenPileController.class);

    @Autowired
    private HlhtTenantOpenPileService hlhtTenantOpenPileService;

    @Autowired
    private HlhtTenantOpenService hlhtTenantOpenService;

    @Autowired
    private HlhtTenantOpenGunService hlhtTenantOpenGunService;

    @Autowired
    private ElecPileService elecPileService;

    @Autowired
    private ElecGunService elecGunService;


    @ResponseBody
    @RequestMapping(value = "/listPageHlhtTenantOpenPile", method = RequestMethod.GET)
    public WebPageInfo listPageHlhtTenantOpenPile(HlhtTenantOpenPileSearch hlhtTenantOpenPileSearch){
        PageInfo<HlhtTenantOpenPileVo> pageInfo = hlhtTenantOpenPileService.listPageHlhtTenantOpenPile(hlhtTenantOpenPileSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    @ResponseBody
    @RequestMapping(value = "/updateOrSaveHlhtTenantOpenPile", method = RequestMethod.POST)
    public TResponse updateOrSaveHlhtTenantOpenPile(@RequestParam("openPileIdList") List<Long> openPileIdList,
                                                    @RequestParam("elecPileIdList") List<Long> elecPileIdList,
                                                    @RequestParam("tenantOpenId") Long tenantOpenId,
                                                    @RequestParam("openStatus") String openStatus,
                                                    @RequestParam("epHlhtOpenforType") String epHlhtOpenforType){
        TenantUser user = ShiroUtils.getSessionUser();
        HlhtTenantOpen hlhtTenantOpen = hlhtTenantOpenService.selectByPrimaryKey(tenantOpenId);
        try{
            //已开放的桩list
            List<Long> openedElecPileIdList = new ArrayList<>();
            if (openPileIdList != null && openPileIdList.size() > 0){
                openedElecPileIdList = hlhtTenantOpenPileService.getElecPileIdByOpenPile(openPileIdList);
            }
            //openStatus 0开放 1关闭
            if ("1".equals(openStatus) && openPileIdList != null && openPileIdList.size() > 0){
                hlhtTenantOpenPileService.deleteByBatch(openPileIdList);
                hlhtTenantOpenGunService.deleteBatchByElecPileIdList(hlhtTenantOpen, openedElecPileIdList);
                return TResponse.SUCCESS("关闭成功");
            }

            if (openPileIdList != null && openPileIdList.size() > 0){
                //批量更新桩的hlhtOpenforType
                hlhtTenantOpenPileService.updateHlhtOpenforTypeBatchByOpenPileIdList(openPileIdList, epHlhtOpenforType);
            }

            if (openedElecPileIdList != null && openedElecPileIdList.size() > 0){
                hlhtTenantOpenGunService.updateGunHlhtOpenforTypeBatchByOpePileIdList(hlhtTenantOpen, openedElecPileIdList, epHlhtOpenforType);
            }

            if (elecPileIdList != null && elecPileIdList.size() > 0){
                List<ElecPile> elecPileList = elecPileService.listElecPileByElecPileIdList(elecPileIdList);
                List<ElecGun> elecGunList = elecGunService.listElecGunByElecPileIdList(elecPileIdList);
                //批量插入hlhtTenantOpenPile
                if (elecPileList != null && elecPileList.size() > 0){
                    hlhtTenantOpenPileService.insertBatchByElecPileList(hlhtTenantOpen, elecPileList,epHlhtOpenforType, user.getLoginName());
                }

                //批量插入hlhtTenantOpenGun
                if (elecGunList != null && elecGunList.size() > 0){
                    hlhtTenantOpenGunService.insertBatchByElecGunList(hlhtTenantOpen, elecGunList, epHlhtOpenforType, user.getLoginName());
                }
            }
        }catch (Exception e){
            logger.error("HlhtTenantOpenPileController.updateOrSaveHlhtTenantOpenPile:{}", e);
            return TResponse.FAIL("操作失败");
        }

        return TResponse.SUCCESS();


    }
}
